﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.Model;
using YouHP.BLL;
using YouHP.Utility;
using System.Web;
using YouHP.Model.Group;

namespace YouHP.Pages.User.Group
{
    public class GroupInvite:UserBasePage
    {
        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HaddleCommon();
            if (ErrorCount > 0) return;

            if (IsPost)
                HaddlePost();
            else
                HaddleGet();
        }

        protected List<GroupInviteInfo> NewGroupInviteList;
        protected int pageSize = 10;
        protected int pageIndex = 1;
        protected int pageCount = 0;
        protected int dataCount;
        protected string PageNumberString;

        private void HaddleGet()
        {
            pageIndex = RequestTools.GetQueryInt("page", 1);
            NewGroupInviteList = GroupBLL.Instance.GetNewGroupInviteListByUserId(pageSize, pageIndex, UserData.Id, out dataCount);

            pageCount = Utils.GetPageCount(dataCount, pageSize);

            string query = HttpContext.Current.Request.Url.Query;
            query = Utils.RemoveQueryItem(query, "page");

            PageNumberString = this.GetPagingString(pageIndex, pageCount,
                "/User/Group/GroupInvite.aspx" + query, "page");
        }

        private void HaddlePost()
        {
            int id = RequestTools.GetFormInt("inviteid", 0);
            int actiontype = RequestTools.GetFormInt("actiontype", -1);
            int gid = RequestTools.GetFormInt("groupid", 0);


            GroupInfo GroupData=GroupBLL.Instance.GetGroupInfo(gid);
            if(GroupData.Id<=0)
            {
                this.AddErrLine("圈子不存在！");
                return;
            }

            bool succ = false;

            switch (actiontype)
            {
                case 0:

                    succ = GroupBLL.Instance.DelGroupInvite(id);
                    if (succ)
                    {
                        this.Redirect("/User/Group/GroupInvite.aspx");
                    }
                    else
                    {
                        this.AddErrLine("操作失败");
                        return;
                    }
                    break;
                case 1:

                    succ = GroupUserBLL.Instance.UpdateGroupUserRole(GroupData.Id,UserData.Id,Constants.RoleMember);
                    bool succ2 = GroupBLL.Instance.DelGroupInvite(id);

                    if (succ&&succ2)
                    {
                        this.AddMsgLine("操作成功，已经加入该圈！");
                        this.SetUrl("/User/Group/GroupInvite.aspx");
                        this.SetMetaRefresh();
                    }
                    else
                    {
                        this.AddErrLine("操作失败");
                        return;
                    }
                    break;
            }
            
        }

        private void HaddleCommon()
        {
            this.AddLocation("圈子邀请", "/User/Group/NewGroupInvite.aspx");
            this.SetTitle("圈子邀请");
        }
    }
}
